package javacore.vch3;

import netscape.javascript.JSObject;

import java.util.*;
import java.util.stream.Collectors;

public class CollectionTest {

    static List<Student> getStudents() {
        List<Student> students = new ArrayList<>();

        for (int i = 0; i < 30; i ++) {
            Student student = new Student();
            student.id = String.valueOf(i);
            student.age = i % 5 ;
            student.sex = i % 2 == 0 ? "男" : "女";
            student.name = "张三" + i;
            students.add(student);
        }
//        Student student = new Student();
//        student.id = "3";
//        student.age = 3 ;
//        student.sex = "男";
//        student.name = "张三";
//        students.add(student);
//
//       student = new Student();
//        student.id = "3";
//        student.age = 3 ;
//        student.sex = "男";
//        student.name = "张三";
//        students.add(student);
//
//        student = new Student();
//        student.id = "3";
//        student.age = 3 ;
//        student.sex = "男";
//        student.name = "张三";
//        students.add(student);
//
//        student = new Student();
//        student.id = "3";
//        student.age = 4 ;
//        student.sex = "男";
//        student.name = "张三";
//        students.add(student);
//
//        student = new Student();
//        student.id = "3";
//        student.age = 4 ;
//        student.sex = "男";
//        student.name = "张三";
//        students.add(student);
        return students;
    }

    public static void testForeach () {
        List<Student> students = getStudents();
        List<Student> result = new ArrayList<>();
        result.add(students.get(0));
        for (Student s : students) {
            for (Student s2: result) {
                System.out.println("s1:"+s.toString() + ",s2:" +s.toString());
                if(s2.id == s.id) {
                    break;
                } else {
                    result.add(s);
                    break;
                }
            }
        }
        for (Student s: result) {
            System.out.println(s.toString());
        }
    }

    public static void mapDefault() {
        Map<String, Integer> map = new HashMap<>();
        int count = map.get("a");
        System.out.println(count);
    }

    public static void main(String[] args) {
        List<Student> students = getStudents();
//        Map<String, Long> group1 = students.stream().collect(Collectors.groupingBy(student -> student.sex, Collectors.counting()));

//        Map<Integer, Long> group2 = students.stream().collect(Collectors.groupingBy(student -> student.age, Collectors.counting()));
//        List<Map2Entity> list = group2.entrySet().stream().sorted(Comparator.comparing(Map.Entry<Integer, Long>::getValue).reversed())
//                .map(e -> new Map2Entity(String.valueOf(e.getKey()), e.getValue())).collect(Collectors.toList());
//        List<Map.Entry<String, Long>> list = new ArrayList<Map.Entry<String, Long>>(group2.entrySet());
        //        for (String key: group1.keySet()) {
//            System.out.println(key + "," + group1.get(key));
//        }
//        for (int key: group2.keySet()) {
//            System.out.println(key + "," + group2.get(key));
//        }
//
//        for (Map2Entity entity: list) {
//            System.out.println(entity.toString());
//        }
//        testForeach();

        mapDefault();
    }

}


class Student {
    String id;
    int age;
    String sex;
    String name;
}

class Map2Entity {
    private String key;
    private Object value;

    public Map2Entity (String key, Object value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return this.getKey() + "，" + this.getValue();
    }
}
